import { _decorator, Component, Node, Sprite, Label, Color, v3, tween, UITransform } from 'cc';
const { ccclass, property } = _decorator;

import SSS_TimerManager from "../../../Script/Manager/SSS_TimerManager";
import { S2048_GameData } from "./S2048_GameData";

@ccclass('S2048Block')
export default class S2048_Block extends Component {
    @property(Node)
    normal: Node | null = null;
    @property(Sprite)
    icon: Sprite | null = null;
    @property(Label)
    Label_point: Label | null = null;
    col: number = 0;
    row: number = 0;
    length: number = 1;
    //    // color;
    //    // Label_color;
    id: number;
    isComplete: boolean = false;
    目标 = {
        id: 0,
        col: 0,
        row: 0,
    };
    //    // 只序列化需要的属性
    toJSON() {
        return {
            col: this.col,
            row: this.row,
            id: this.id,
            // _class: 'MyClass' // 添加类标识以便反序列化
        };
    }
    //    // 反序列化方法
    static fromJSON(data: any) {
        // return new S2048_Block(data.id, data.col, data.row);
    }
    //    // 初始化
    init(_col: number, _row: number, _id: number) {
        this.col = _col;
        this.row = _row;
        this.id = _id;

        // console.log(`x,y `, this.x, this.y);

        // let item_data = S2048_GameData.colors.find(e => e.id == this.id);
        // this.color = item_data.color;
        // this.Label_color = item_data.Label_color;

        this.node.active = true;
        this.InitWidthAndHeight(S2048_GameData.size, S2048_GameData.size);
        this.InitPosition();

        this.Refresh();
    }
    async Refresh() {
        this.Refresh_number();
        this.Refresh_color();
    }
    async Refresh_number() {
        this.Label_point.string = `${this.id}`;

        let item_data = S2048_GameData.colors.find(e => e.id == this.id);
        let Label_color = item_data.Label_color;
        this.Label_point.color = new Color().fromHEX(Label_color);
    }
    async Refresh_color() {
        // console.log(`color`, this.color);

        let item_data = S2048_GameData.colors.find(e => e.id == this.id);
        let color = item_data.color;
        this.icon.color = new Color().fromHEX(color);
        // console.log(`this.icon.node.color`, this.icon.node.color);
    }
    //    // 初始化单元宽高
    InitWidthAndHeight(cardWidth?: number, cardHeight?: number) {
        if (cardWidth) this.node.getComponent(UITransform).width = cardWidth;
        if (cardHeight) this.node.getComponent(UITransform).height = cardHeight;
    }
    //    // 初始化位置
    InitPosition() {
        let x = -S2048_GameData.W / 2 + this.node.getComponent(UITransform).width / 2 + this.node.getComponent(UITransform).width * (this.col - 1);
        let y = -S2048_GameData.H / 2 + this.node.getComponent(UITransform).height / 2 + this.node.getComponent(UITransform).height * (this.row - 1);
        this.node.position = v3(x, y, 0);
    }
    async MovePosition(_duration = 0.1) {
        let x = -S2048_GameData.W / 2 + this.node.getComponent(UITransform).width / 2 + this.node.getComponent(UITransform).width * (this.col - 1);
        let y = -S2048_GameData.H / 2 + this.node.getComponent(UITransform).height / 2 + this.node.getComponent(UITransform).height * (this.row - 1);
        let position = v3(x, y, 0);
        tween(this.node)
            .to(_duration, { position: position })
            .call(() => {
                // this.Refresh();
            })
            .start();

        await new Promise(res => SSS_TimerManager.Instance.scheduleOnce(res, _duration));
    }
    // Destroy() {
    //     this.node.destroy();
    // }
}